

package com.ruoyi.system.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

import org.apache.poi.ss.usermodel.DateUtil;

public class LocalDateConverter implements Converter<LocalDate> {
    public LocalDateConverter() {
    }

    public Class<?> supportJavaTypeKey() {
        return LocalDate.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
        if (null == cellData) {
            return null;
        } else {
            LocalDate result = null;
            if (cellData.getType() == CellDataTypeEnum.NUMBER) {
                Date date;
                if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
                    date = DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), contentProperty.getDateTimeFormatProperty().getUse1904windowing(), (TimeZone) null);
                    result = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                } else if (cellData.getNumberValue().toString().length() == 8) {
                    result = LocalDate.parse(cellData.getNumberValue().toString(), DateTimeFormatter.ofPattern("yyyyMMdd"));
                } else {
                    date = DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(), globalConfiguration.getUse1904windowing(), (TimeZone) null);
                    result = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                }
            }

            if (cellData.getType() == CellDataTypeEnum.STRING) {
                String value = cellData.getStringValue();
                if (value.contains("-")) {
                    result = LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                } else if (value.contains("/")) {
                    result = LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy/MM/dd"));
                } else if (value.length() == 8) {
                    result = LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyyMMdd"));
                }
            }

            return result;
        }
    }

    public WriteCellData<LocalDate> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null == value ? new WriteCellData() : new WriteCellData(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    }
}
